package com.hongdee.crm.base.controller;

import com.hongdee.base.entity.ResultEntity;
import com.hongdee.crm.Template.entity.TCrmTemplateRef;
import com.hongdee.crm.base.entity.TCrmSurveyGroup;
import com.hongdee.crm.base.service.ITCrmBaseSurveryService;
import com.hongdee.crm.wo.service.ITCrmWoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

/**
 * Created by dell on 2017/5/10.
 */
@RestController
@RequestMapping("/crm/base/survery")
public class BaseSurveryController {
    @Autowired
    private ITCrmBaseSurveryService itCrmBaseSurveryService;
    @RequestMapping("/group/save")
    @ResponseBody
    public ResultEntity save(@RequestParam(name = "uid", defaultValue = "") String uid,
                             @RequestParam(name = "token", defaultValue = "") String token,
                             @RequestBody TCrmSurveyGroup tCrmSurveyGroup) throws Exception {
        System.out.println("我接到的实体里面有"+tCrmSurveyGroup);
        TCrmSurveyGroup group = itCrmBaseSurveryService.saveOrUpdate(uid,tCrmSurveyGroup);
        ResultEntity res = new ResultEntity();
        res.setData(group);
        res.setSuccess(true);
        res.setMsg("保存成功！");
        return res;
    }
    /*
    * 查询
    * */
    @RequestMapping("/group/info")
    @ResponseBody
    public ResultEntity info(@RequestParam(name = "uid", defaultValue = "") String uid,
                             @RequestParam(name = "token", defaultValue = "") String token,
                             @RequestParam(name = "id", defaultValue = "") String id) throws Exception {
        ResultEntity res = new ResultEntity();
        Map map = itCrmBaseSurveryService.info(id);
        res.setData(map);
        res.setSuccess(true);
        res.setMsg("查询详情成功");
        return res;
    }
    /**
     * 停用
     */
    @RequestMapping("/group/stop")
    @ResponseBody
    public ResultEntity stop(@RequestParam(name = "uid", defaultValue = "") String uid,
                             @RequestParam(name = "token", defaultValue = "") String token,
                             @RequestParam(name = "id", defaultValue = "0") String id) throws Exception {
        ResultEntity res = new ResultEntity();
        itCrmBaseSurveryService.update(id, "0");
        res.setSuccess(true);
        res.setMsg("停用成功！");
        return res;
    }
    /**
     * 启用
     */
    @RequestMapping("/group/start")
    @ResponseBody
    public ResultEntity start(@RequestParam(name = "uid", defaultValue = "") String uid,
                              @RequestParam(name = "token", defaultValue = "") String token,
                              @RequestParam(name = "id", defaultValue = "0") String id) throws Exception {
        ResultEntity res = new ResultEntity();
        itCrmBaseSurveryService.update(id, "1");
        res.setSuccess(true);
        res.setMsg("启动成功！");
        return res;
    }

    /*
    * 分页
    * */
    @RequestMapping("/group/page")
    @ResponseBody
    public Map newPage(@RequestParam(name = "uid", defaultValue = "") String uid,
                       @RequestParam(name = "token", defaultValue = "") String token,
                       @RequestParam(name = "start", defaultValue = "0") String start,
                       @RequestParam(name = "limit", defaultValue = "50") String limit,
                       @RequestParam(name = "groupCode", defaultValue = "") String groupCode
    ) throws Exception {
        Map map = this.itCrmBaseSurveryService.newPage(start, limit, groupCode);
        map.put("success", true);
        map.put("msg", "分页查询成功！");
        return map;
    }
}
